package com.simple.component;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.billy.cc.core.component.CC;
import com.billy.cc.core.component.CCResult;
import com.billy.cc.core.component.IComponentCallback;
import com.simple.R;

/**
 * Copyright (C), 2016-2020,呼我出行网络科技有限公司
 * FileName: LauncherActivity
 * Author: Administrator
 * Date: 2020/11/3 11:00
 * Description:
 * History:
 */
public class LauncherActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        findViewById(R.id.info_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callComponentMy(Global.EXTRA_DATA, "查看信息");
            }
        });
        findViewById(R.id.order_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //toTarget(ComponentAction.Component_ORDER, ComponentAction.ACTION_ORDER, "订单页面");
                callComponentOrder(Global.EXTRA_DATA, "订单页面");
            }
        });
        findViewById(R.id.buy_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toTarget(ComponentAction.Component_BUY, ComponentAction.ACTION_BUY, "买买买买买");
            }
        });
    }

    /**
     * 模拟调多个组件
     *
     * @param actionName
     * @param data
     */
    private void toTarget(String componentName, String actionName, String data) {
        CcCall.callComponent(componentName, actionName, "data", data);
    }

    /**
     * 测试拦截器
     *
     * @param extraKey
     * @param extraValue
     */
    public static void callComponentMy(String extraKey, Object extraValue) {
        CC.Builder builder = CcCall.newBuilder(ComponentAction.Component_My, ComponentAction.ACTION_MY);
        builder.addInterceptor(new LoginCcInterceptor());
        if (!TextUtils.isEmpty(extraKey)) {
            builder.addParam(extraKey, extraValue);
        }

        builder.build().callAsync();
    }

    /**
     * 去订单页面
     *
     * @param extraKey
     * @param extraValue
     */
    public static void callComponentOrder(String extraKey, Object extraValue) {
        CcCall.callComponentAsyncMain(
                ComponentAction.ACTION_LOGIN,
                ComponentAction.ACTION_LOGIN, false, new IComponentCallback() {

                    @Override
                    public void onResult(CC cc, CCResult result) {
                        Log.e("tgl===", "result: " + result.toString());
                        CCResult ccResult;
                        // 根据登录状态决定是否打开页面
                        // 这里也可以添加更多的前置判断逻辑
                        if (result.isSuccess()) {
                            ccResult = CCResult.success();
                            //登录成功，打开目标页面

                            CC.Builder builder = CcCall.newBuilder(ComponentAction.Component_ORDER,
                                    ComponentAction.ACTION_ORDER);
                            if (!TextUtils.isEmpty(extraKey)) {
                                builder.addParam(extraKey, extraValue);
                                builder.addParam(Global.KEY_USERNAME,
                                        ccResult.getDataItem(Global.KEY_USERNAME));
                            }
                            builder.build().call();

                        } else {
                            //登录失败，返回失败信息
                            ccResult = result;
                        }
                        CC.sendCCResult(cc.getCallId(), ccResult);
                    }
                }, Global.EXTRA_DATA, extraValue,
                "return", true);

    }
}
